//index.js
const app = getApp()
const {
    envList
} = require('../../envList.js')

Page({
    data: {
        red: [],
        blue: [],
        group: 'gender',
        redGroup: '',
        blueGroup: ''

    },

    onLoad: function (options) {
        // Do some initialize when page load.
        console.log(options)
        wx.showLoading({
            title: '加载中',
        })
        wx.cloud.callFunction({
            // 云函数名称
            name: 'classCounter',
            // 传给云函数的参数
            data: {
                type: 'counterList',
                num: options['num'] ? options['num'] : 1,
                group: options['group'],
                redGroup: options['redGroup'],
                blueGroup: options['blueGroup']
            },
        })
            .then(res => {
                if (res.result.success) {
                    console.log(res.result.data.student) // 3
                    this.setData({
                        red: res.result.data.student.red,
                        blue: res.result.data.student.blue,
                        group: options['group'],
                        redGroup: options['redGroup'],
                        blueGroup: options['blueGroup']
                    })
                } else {
                    wx.showToast({
                        title: res.result.msg,
                        icon: 'error',
                        duration: 2000
                    })

                }
                wx.hideLoading()
            })
            .catch(console.error)
    },
    // Event handler.
    checkboxChange: function (e) {
        console.log(e.detail.value)
    },
    jumpPage(e) {
        wx.navigateTo({
            url: '/pages/counterhandle/index',
        })
    },
    formSubmit(e) {
        let winner = []
        console.log(this.data)
        if (e.detail.value.redPassNames.length > e.detail.value.bluePassNames.length) {
            winner = [this.data.redGroup]
        } else if (e.detail.value.redPassNames.length < e.detail.value.bluePassNames.length) {
            winner = [this.data.blueGroup]
        } else {
            winner = [this.data.redGroup, this.data.blueGroup]
        }
        wx.showLoading({
            title: '加载中',
        })
        wx.cloud.callFunction({
            name: 'classCounter',
            data: {
                type: 'upScore',
                ids: 0,
                group: this.data.group,
                winner: winner
            }
        }).then(res => {
            if (res.result.success) {
                wx.showToast({
                    title: res.result.data,
                    icon: 'success',
                    duration: 2000,
                }).then(res => {
                    setTimeout(function () {
                        wx.navigateBack({
                            delta: 1,
                        })
                    }, 2000)
                })
            } else {
                wx.showToast({
                    title: res.result.data,
                    icon: 'error',
                    duration: 2000
                })
            }
            setTimeout(function () {
                wx.hideLoading()
            }, 2000)
        }).catch(console.error)
    },

    formReset(e) {
        //this.jumpPage();
    },
})